#ifndef LOADOBJ_H
#define LOADOBJ_H

#include <QWidget>
#include <QPointer>
#include <QGuiApplication>
#include <QLayout>
#include <QMouseEvent>
#include <QObject>

#include <QtGui/QScreen>
#include <Qt3DCore/qentity.h>
#include <Qt3DCore/qtransform.h>
#include <Qt3DCore/qaspectengine.h>
#include <Qt3DRender/qcameralens.h>
#include <QtWidgets/QApplication>
#include <QtWidgets/QWidget>
#include <QtWidgets/QHBoxLayout>
#include <QtWidgets/QCheckBox>
#include <QtWidgets/QCommandLinkButton>
#include <Qt3DInput/QInputAspect>
#include <Qt3DRender/qmesh.h>
#include <Qt3DRender/qsceneloader.h>
#include <Qt3DRender/qtechnique.h>
#include <Qt3DRender/qmaterial.h>
#include <Qt3DRender/qeffect.h>
#include <Qt3DRender/qtexture.h>
#include <Qt3DRender/qrenderpass.h>
#include <Qt3DRender/qsceneloader.h>
#include <Qt3DRender/qpointlight.h>
#include <Qt3DRender/qdirectionallight.h>
#include <Qt3DRender/qspotlight.h>
#include <Qt3DRender/qrenderaspect.h>
#include <Qt3DRender/qcamera.h>
#include <Qt3DRender/qobjectpicker.h>
#include <Qt3DRender/qpickevent.h>
#include <Qt3DExtras/qtorusmesh.h>
#include <Qt3DExtras/qforwardrenderer.h>
#include <Qt3DExtras/qt3dwindow.h>
#include <Qt3DExtras/qfirstpersoncameracontroller.h>
#include <Qt3DExtras/QPhongMaterial>
#include <Qt3DExtras/QCylinderMesh>

#include <Qt3DExtras/QTextureMaterial>
#include <Qt3DExtras/QPlaneMesh>
#include <Qt3DRender/QLayer>
//#include <Qt3DRender/QMaterialProperties>

#include "self3dwindow.h"

//直接用下面的形式包含类，也是可以的，但是编译启动慢
//#include <Qt3DRender>
//#include <Qt3DCore>
//#include <QtWidgets>
//#include <QtGui/QScreen>
//#include <Qt3DInput>
//#include <Qt3DExtras>
//#include <Qt3DRender>

/** 声明类的别名 */
#define STATEMENT_CLASS_ALIAS_NAME(classname,aliasname) typedef classname aliasname

namespace Ui {
class LoadObj;
}

class LoadObj : public QWidget
{
    Q_OBJECT

public:
    explicit LoadObj(QWidget *parent = Q_NULLPTR);
    ~LoadObj();
    void initializateOn3DView();
    void load3DModel(const QString&obj_url);
    void reloadModel();

    bool eventFilter(QObject *obj, QEvent *event);

signals:
    void sigTransPos(int,int,int);

public slots:
    void slotChangeBackColor(QColor color);
    void slotChangeLightColor(QColor color);
    void slotChangeLightSource(int source);
    void slotChangeLightIntensity(int val);
    void slotRotateX(int val);
    void slotRotateY(int val);
    void slotRotateZ(int val);
    void slotTransPos(int xpos,int ypos);   //传递鼠标单击点的坐标
    void slotMouseDrag(int xOff,int yOff);  //鼠标拖动功能处理
    void slotZoom(int flag);           //鼠标滚轮滚动实现模型缩放
    void slotChangeMoveFunc(int func); //鼠标拖动的功能
    void slotLoadStat(Qt3DRender::QSceneLoader::Status status);   //模型加载完成状态

    void slotPickerPressed(Qt3DRender::QPickEvent *pickEvent);
    void slotPickerMoved(Qt3DRender::QPickEvent *pickEvent);
    void slotPickerReleased(Qt3DRender::QPickEvent *pickEvent);
    void slotPickerClicked(Qt3DRender::QPickEvent *pickEvent);
    void slotPickerExited();    //不能带参数Qt3DRender::QPickEvent *pickEvent，否则编译会报错

    void slotLightRotateX(int val);  //X轴光照旋转
    void slotLightRotateY(int val);  //Y轴光照旋转
    void slotLightRotateZ(int val);  //Z轴光照旋转

protected:
    void mouseMoveEvent(QMouseEvent *event);
    void mousePressEvent(QMouseEvent *event);

private:
    Ui::LoadObj *ui;

    STATEMENT_CLASS_ALIAS_NAME(Qt3DExtras::Qt3DWindow, Qt3DWindow);
    STATEMENT_CLASS_ALIAS_NAME(Qt3DCore::QEntity, QtEntity);
    STATEMENT_CLASS_ALIAS_NAME(Qt3DRender::QMaterial, QtMaterial);
    STATEMENT_CLASS_ALIAS_NAME(Qt3DCore::QTransform, QtTransform);
    STATEMENT_CLASS_ALIAS_NAME(Qt3DRender::QCamera, QtCamera);
    STATEMENT_CLASS_ALIAS_NAME(Qt3DInput::QInputAspect, QtInputAspect);
    STATEMENT_CLASS_ALIAS_NAME(Qt3DRender::QObjectPicker, QtObjectPicker);
    STATEMENT_CLASS_ALIAS_NAME(Qt3DRender::QSceneLoader, QtSceneLoader);

    struct date_struct
    {
        //QPointer<Qt3DWindow> m_windowOn3D;
        QPointer<Self3DWindow> m_windowOn3D;
        QPointer<QtEntity> m_rootEntity;
        QPointer<QtMaterial> m_material;
        QPointer<QtTransform> m_trans;
        QPointer<QtInputAspect> m_input;
        QPointer<QtCamera> m_camera;
        QPointer<QtObjectPicker> m_picker;
        QPointer<QtSceneLoader> m_scene;

        QPointer<QtEntity> m_lightEntity;
        QPointer<QtTransform> m_lightTrans;
    }db;

    QColor m_backColor;
    QColor m_lightColor;
    int m_lightSource; //光源，1点光2定向光3聚集光
    float m_lightIntensity;
    int m_XAngle;  //X轴旋转角度
    int m_YAngle;  //Y轴旋转角度
    int m_ZAngle;  //Z轴旋转角度
    float m_zoomVal;  //模型缩放比例
    int m_moveFunc;   //鼠标拖动的功能选择：1旋转2平移
    float m_XTrans; //X轴方向平移观察
    float m_YTrans; //Y轴方向平移观察
    float m_ZTrans; //Z轴方向平移观察

    float m_XLigRota;  //X轴光照旋转
    float m_YLigRota;  //Y轴光照旋转
    float m_ZLigRota;  //Z轴光照旋转
};

#endif // LOADOBJ_H
